package ar.com.jwa.domain;

import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

@SuppressWarnings("restriction")
public class Mail {
	
	private String host;
	
	private String from;
	
	private String to;
	
	private Properties props;
	
	private Session session;
	
	private MimeMessage message;
	
	private String text;
	
	private String subject;
	
	private BodyPart messageBodyPart;
	
	private String protocol;
	
	private String smtpUser;
	
	private String smtpPassword;
	
	private MimeMultipart multipart;
	

	public Mail(String host, String from,String subject,String protocol, String smtpUser, String smtpPassword) {
		super();
		this.props = System.getProperties();
		this.session = Session.getDefaultInstance(props, null);
		this.message = new MimeMessage(session);
		this.host = host;
		this.from = from;
		this.subject = subject;
		this.protocol = protocol;
		this.smtpUser = smtpUser;
		this.smtpPassword = smtpPassword;
		this.multipart = new MimeMultipart("related");
	}
	
	public Mail(){
		this.props = System.getProperties();
		this.session = Session.getDefaultInstance(props, null);
		this.message = new MimeMessage(session);
		this.messageBodyPart = new MimeBodyPart();
		this.multipart = new MimeMultipart("related");
	}

	private void send() throws AddressException, MessagingException {
		
		this.props.put("mail.smtp.host",this.getHost());
		this.props.put("mail.smtp.user",this.getSmtpUser());
		this.props.put("mail.smtp.pass",this.getSmtpPassword());
		this.props.put("mail.smtp.starttls.enable","true");
		this.props.put("mail.smtps.auth", "true");  
		this.props.put("mail.smtps.quitwait", "false");
		this.props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		this.props.put("mail.smtp.socketFactory.fallback", "false");
		this.props.put("mail.transport.protocol",this.getProtocol());
		
		this.message.setFrom(new InternetAddress(this.getFrom()));
		this.message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.getTo()));
		this.message.setSubject(this.getSubject());
		 
		Transport t = session.getTransport(this.protocol);

		t.connect(this.host,(String) props.get("mail.smtp.user"),(String)props.get("mail.smtp.pass"));
		
		t.sendMessage(this.message, this.message.getAllRecipients());

		t.close();
	}
	
	
	public void sendAsHtml() throws MessagingException, IOException{

		String file = "/home/hgonzalez/desarrollo/SUSCRIPCIONES2/WeeklySoccer/WebContent/images/soccer2.jpg";
		this.getMessageBodyPart().setContent(this.getText(), "text/html");
		this.getMultipart().addBodyPart(this.getMessageBodyPart());
		
		messageBodyPart = new MimeBodyPart();
		
		DataSource fds = new FileDataSource(file);
		
		this.messageBodyPart.setDataHandler(new DataHandler(fds));
		this.messageBodyPart.setHeader("Content-ID", "soccer");
		this.message.setContent(this.getMultipart());
		this.message.setContent(this.getText(),"text/html");
		this.multipart.addBodyPart(messageBodyPart);
		this.message.setContent(multipart);
		
		this.send();
		
	}
	
	public void sendAsPlainText() throws AddressException, MessagingException{
		this.send();
	}
	
	/**
	 * @return the message
	 */
	public MimeMessage getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(MimeMessage message) {
		this.message = message;
	}
	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}
	/**
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}
	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}
	/**
	 * @param from the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}
	/**
	 * @return the to
	 */
	public String getTo() {
		return to;
	}
	/**
	 * @param to the to to set
	 */
	public void setTo(String to) {
		this.to = to;
	}
	/**
	 * @return the props
	 */
	public Properties getProps() {
		return props;
	}
	/**
	 * @param props the props to set
	 */
	public void setProps(Properties props) {
		this.props = props;
	}
	/**
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}
	/**
	 * @param session the session to set
	 */
	public void setSession(Session session) {
		this.session = session;
	}


	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}


	/**
	 * @param text the text to set
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * @return the messageBodyPart
	 */
	public BodyPart getMessageBodyPart() {
		return messageBodyPart;
	}

	/**
	 * @param messageBodyPart the messageBodyPart to set
	 */
	public void setMessageBodyPart(BodyPart messageBodyPart) {
		this.messageBodyPart = messageBodyPart;
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the protocol
	 */
	public String getProtocol() {
		return protocol;
	}

	/**
	 * @param protocol the protocol to set
	 */
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	/**
	 * @return the smtpUser
	 */
	public String getSmtpUser() {
		return smtpUser;
	}

	/**
	 * @param smtpUser the smtpUser to set
	 */
	public void setSmtpUser(String smtpUser) {
		this.smtpUser = smtpUser;
	}

	/**
	 * @return the smtpPassword
	 */
	public String getSmtpPassword() {
		return smtpPassword;
	}

	/**
	 * @param smtpPassword the smtpPassword to set
	 */
	public void setSmtpPassword(String smtpPassword) {
		this.smtpPassword = smtpPassword;
	}

	/**
	 * @return the multipart
	 */
	public MimeMultipart getMultipart() {
		return multipart;
	}

	/**
	 * @param multipart the multipart to set
	 */
	public void setMultipart(MimeMultipart multipart) {
		this.multipart = multipart;
	}
	
	
}
